﻿using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

namespace TinyAsset.Editor
{
    [CreateAssetMenu]
    public class CollectionSetting : ScriptableObject
    {
        public List<FileCollectionRule> assetBundles;

        public List<FileCollectionRule> raws;

        public List<SceneAsset> buildinScenes;

        public List<string> buildinTags = new List<string>() { AssetBundleUtility.kBuildin };
    }

    [System.Serializable]
    public enum FileCollectionType
    {
        TopDirectoryOnly,

        Directory,

        File,
    }

    [System.Serializable]
    public class FileCollectionRule
    {
        public string path;

        public string tag = AssetBundleUtility.kBuildin;

        public FileCollectionType type;

        public List<FileCollectionResult> Collect()
        {
            List<FileCollectionResult> results = new List<FileCollectionResult>();
            var fullName = Path.Combine(Application.dataPath, path);
            DirectoryInfo directoryInfo = new DirectoryInfo(fullName);

            if ((directoryInfo.Attributes & FileAttributes.Archive) == FileAttributes.Archive)
            {
                if (IsValidExtension(directoryInfo.Extension.ToLower()))
                {
                    results.Add(new FileCollectionResult()
                    {
                        assetPath = directoryInfo.FullName.Substring(Application.dataPath.Length + 1).Replace("\\", "/"),
                        fileCollectionRule = this,
                    });
                }
            }

            if ((directoryInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory)
            {
                FileInfo[] fileInfos = directoryInfo.GetFiles("*.*", SearchOption.AllDirectories);
                foreach (var fileInfo in fileInfos)
                {
                    if (!IsValidExtension(fileInfo.Extension.ToLower()))
                        continue;

                    results.Add(new FileCollectionResult()
                    {
                        assetPath = fileInfo.FullName.Substring(Application.dataPath.Length + 1).Replace("\\", "/"),
                        fileCollectionRule = this,
                    });
                }
            }

            return results;
        }

        bool IsValidExtension(string ext)
        {
            return ext != ".meta" && ext != ".cs";
        }
    }

    public class FileCollectionResult
    {
        public string assetPath;

        public FileCollectionRule fileCollectionRule;

        public string GetAssetBundleName(string extension = null)
        {
            string name = null;
            if (fileCollectionRule.type == FileCollectionType.File)
                name = assetPath.Substring(0, assetPath.LastIndexOf(".")).ToLower();
            if (fileCollectionRule.type == FileCollectionType.Directory)
                name = fileCollectionRule.path.TrimEnd('/').ToLower();
            if (fileCollectionRule.type == FileCollectionType.TopDirectoryOnly)
            {
                string fileName = assetPath.Substring(fileCollectionRule.path.Length);
                int index = fileName.IndexOf('/');
                if (index == -1)
                    name = fileCollectionRule.path.TrimEnd('/').ToLower();
                else
                    name = Path.Combine(fileCollectionRule.path + fileName.Substring(0, index)).Replace("\\", "/").ToLower();
            }
            if (name != null && extension != null)
                name = name + extension;
            return name;
        }
    }
}